<?php namespace modules\project;
/**
 * @subpackage  \modules\project
 *
 * @copyright   Copyright (C) 2013 - 2014 Mailerm, Inc. All rights reserved.
 */

# Load Configuration
require_once MPATH_MODULES . '/configuration.php';

# Load Modules
require_once MPATH_MODULES . '/mod_user/mod_user.php';	
use \modules\user as mod_user;

# Load Models
require_once MPATH_MODULES . '/shared/model_project.php';

use \shared\model\project as Project;

// Start/Begin Project
//-1 项目已结束
// 0 开始成功
// 1 项目进行中
// 2 项目不可用
// 3 项目暂停中
// 4 服务端异常
function start($params, $method, $app) {
    // Check Role
    if (!mod_user\_user_belong(array(ROLE_MANAGER))) {
        $app->return_error(403, 'Forbidden', 'You don\'t have permission to access this server.');
        exit();
    }
    $id = isset($params['id']) ? $params['id'] : '';
    if (trim($id) == '') {
        $app->return_error(400, 'Bad Request', 'Bad Request, Check Parameters.');
        exit();
    }
    // Get project
    $project = Project\get($id);
    if ($project == null) {
        $result['status'] = 2;
        $app->return_json($result);
        exit();
    }
    if ($project['status'] == PROJECT_STATUS_NORMAL) {
        if ($project['process'] == PROJECT_PROCESS_PREPARE) {
            $affected = Project\start($id);
            $result['status'] = $affected < 0 ? 4 : 0;
            $app->return_json($result);
            exit();
        } else if ($project['process'] == PROJECT_PROCESS_ONGOING) {  // in process
            $result['status'] = 1;
            $app->return_json($result);
            exit();
        } else if ($project['process'] == PROJECT_PROCESS_FINISHED) { // finished!
            $result['status'] = -1;
            $app->return_json($result);
            exit();
        } else {    // unkown process 
            $result['status'] = 4;
            $app->return_json($result);
            exit();
        }
    } else if ($project['status'] == PROJECT_STATUS_PAUSED) {
        $result['status'] = 3;
        $app->return_json($result);
        exit();
    } else {    // unkown status 
        $result['status'] = 4;
        $app->return_json($result);
        exit();
    }    
}

// Stop/End Project
//-1 项目已结束
// 0 结束成功
// 1 项目未开始
// 2 项目不可用
// 3 项目暂停中
// 4 服务端异常
function stop($params, $method, $app) {
    // Check Role
    if (!mod_user\_user_belong(array(ROLE_MANAGER))) {
        $app->return_error(403, 'Forbidden', 'You don\'t have permission to access this server.');
        exit();
    }
    $id = isset($params['id']) ? $params['id'] : '';
    if (trim($id) == '') {
        $app->return_error(400, 'Bad Request', 'Bad Request, Check Parameters.');
        exit();
    }
    // Get project
    $project = Project\get($id);
    if ($project == null) {
        $result['status'] = 2;
        $app->return_json($result);
        exit();
    }
    if ($project['status'] == PROJECT_STATUS_NORMAL) {
        if ($project['process'] == PROJECT_PROCESS_ONGOING) {
            $affected = Project\stop($id);
            $result['status'] = $affected < 0 ? 4 : 0;
            $app->return_json($result);
            exit();
        } else if ($project['process'] == PROJECT_PROCESS_PREPARE) {  // preparing
            $result['status'] = 1;
            $app->return_json($result);
            exit();
        } else if ($project['process'] == PROJECT_PROCESS_FINISHED) { // finished!
            $result['status'] = -1;
            $app->return_json($result);
            exit();
        } else {    // unkown process 
            $result['status'] = 4;
            $app->return_json($result);
            exit();
        }
    } else if ($project['status'] == PROJECT_STATUS_PAUSED) {
        $result['status'] = 3;
        $app->return_json($result);
        exit();
    } else {    // unkown status 
        $result['status'] = 4;
        $app->return_json($result);
        exit();
    }
}

// Pause Project
//-1 项目已结束
// 0 暂停成功
// 1 项目未开始
// 2 项目不可用
// 3 项目暂停中
// 4 服务端异常
function pause($params, $method, $app) {
    // Check Role
    if (!mod_user\_user_belong(array(ROLE_MANAGER))) {
        $app->return_error(403, 'Forbidden', 'You don\'t have permission to access this server.');
        exit();
    }
    $id = isset($params['id']) ? $params['id'] : '';
    if (trim($id) == '') {
        $app->return_error(400, 'Bad Request', 'Bad Request, Check Parameters.');
        exit();
    }
    // Get project
    $project = Project\get($id);
    if ($project == null) {
        $result['status'] = 2;
        $app->return_json($result);
        exit();
    }
    if ($project['status'] == PROJECT_STATUS_NORMAL) {
        if ($project['process'] == PROJECT_PROCESS_ONGOING || $project['process'] == PROJECT_PROCESS_PREPARE) {
            $affected = Project\pause($id);
            $result['status'] = $affected < 0 ? 4 : 0;
            $app->return_json($result);
            exit();
        } else if ($project['process'] == PROJECT_PROCESS_FINISHED) { // finished!
            $result['status'] = -1;
            $app->return_json($result);
            exit();
        } else {    // unkown process 
            $result['status'] = 4;
            $app->return_json($result);
            exit();
        }
    } else if ($project['status'] == PROJECT_STATUS_PAUSED) {
        $result['status'] = 3;
        $app->return_json($result);
        exit();
    } else {    // unkown status 
        $result['status'] = 4;
        $app->return_json($result);
        exit();
    }
}

// Restore Paused Project
//-1 项目已结束
// 0 恢复成功
// 1 项目未开始
// 2 项目不可用
// 4 服务端异常
function restore($params, $method, $app) {
    // Check Role
    if (!mod_user\_user_belong(array(ROLE_MANAGER))) {
        $app->return_error(403, 'Forbidden', 'You don\'t have permission to access this server.');
        exit();
    }
    $id = isset($params['id']) ? $params['id'] : '';
    if (trim($id) == '') {
        $app->return_error(400, 'Bad Request', 'Bad Request, Check Parameters.');
        exit();
    }
    // Get project
    $project = Project\get($id);
    if ($project == null) {
        $result['status'] = 2;
        $app->return_json($result);
        exit();
    }
    if ($project['status'] == PROJECT_STATUS_PAUSED) {
        $affected = Project\restore($id);
        $result['status'] = $affected < 0 ? 4 : 0;
        $app->return_json($result);
        exit();
    } else if ($project['status'] == PROJECT_STATUS_NORMAL) {
        $result['status'] = 0;
        $app->return_json($result);
        exit();
    } else {    // unkown status 
        $result['status'] = 4;
        $app->return_json($result);
        exit();
    }
}

// Add New Project
// 0 添加成功
// 1 输入信息有误
// 4 服务端异常
function add($params, $method, $app) {
    $app->require_method('post');
    // Check Role
    if (!mod_user\_user_belong(array(ROLE_MANAGER))) {
        $app->return_error(403, 'Forbidden', 'You don\'t have permission to access this server.');
        exit();
    }

    $name = isset($params['name']) ? $params['name'] : '';
    $status = PROJECT_STATUS_NORMAL;
    $process = PROJECT_PROCESS_PREPARE;
    $start_time = DEFAULT_DATE;
    $end_time = DEFAULT_DATE;
    $create_time = date(FORMAT_DATE) ;
    $update_time = DEFAULT_DATE;
    $desc = isset($params['desc']) ? $params['desc'] : '';
    if (empty($name)) {
        $result['status'] = 1;
        $app->return_json($result);
        exit();
    }

    if (Project\save($name, $status, $process, $start_time, $end_time, $create_time, $update_time, $desc) < 0) {
        $result['status'] = 4;
        $app->return_json($result);
        exit();
    } else {
        $result['status'] = 0;
        $app->return_json($result);
        exit();
    }
}

// Edit Project
// 0 获取成功
// 1 输入信息有误
// 2 项目不可用
// 4 服务端异常
function edit($params, $method, $app) {
    // Check Role
    if (!mod_user\_user_belong(array(ROLE_MANAGER))) {
        $app->return_error(403, 'Forbidden', 'You don\'t have permission to access this server.');
        exit();
    }
    $id = isset($params['id']) ? $params['id'] : '';
    if (trim($id) == '') {
        $result['status'] = 1;
        $app->return_json($result);
        exit();
    }
    // Get project
    $project = Project\get($id);
    if ($project == null) {
        $result['status'] = 2;
        $app->return_json($result);
        exit();
    }
    if (isset($params['name'])) $project['name'] = $params['name'];
    if (isset($params['desc'])) $project['desc'] = $params['desc'];
    $success = Project\update($project['name'], $project['desc'], $project['id']);
    $result['status'] = $success ? 0 : 4;
    $app->return_json($result);
    exit(); 
}

// Get Project Detail
// 0 获取成功
// 1 输入信息有误
// 2 项目不可用
// 4 服务端异常
function detail($params, $method, $app) {
    // Check Role
    if (!mod_user\_user_belong(array(ROLE_MANAGER))) {
        $app->return_error(403, 'Forbidden', 'You don\'t have permission to access this server.');
        exit();
    }
    $id = isset($params['id']) ? $params['id'] : '';
    if (trim($id) == '') {
        $result['status'] = 1;
        $app->return_json($result);
        exit();
    }
    // Get project
    $project = Project\get($id);
    if ($project == null) {
        $result['status'] = 2;
        $app->return_json($result);
        exit();
    }
    // Check User Status
    if (!isset($project['status']) || $project['status'] == PROJECT_STATUS_DELETED) {
        $result['status'] = 1;
        $app->return_json($result);
        exit(); 
    } else {
        $result['status'] = 0;
        $result['project'] = $project;
        $app->return_json($result);
        exit();
    }
}

// Delete Projects
// 0:操作成功
// 1:输入信息有误
// 4:服务器异常
function delete($params, $method, $app) {
    // Check Role
    if (!mod_user\_user_belong(array(ROLE_MANAGER))) {
        $app->return_error(403, 'Forbidden', 'You don\'t have permission to access this server.');
        exit();
    }
    $ids = isset($params['ids']) ? $params['ids'] : '';
    if (trim($ids) == '') {
        $result['status'] = 1;
        $app->return_json($result);
        exit();
    }
    $tmp = explode(',', $ids);
    $ids = array();
    foreach ($tmp as $k => $v) $ids[] = intval($v);
    if (empty($ids)) {
        $result['status'] = 0;
        $result['affect'] = 0;
        $app->return_json($result);
        exit();   
    }
    // Delete
    $affect = Project\delete($ids);
    # Check Result
    $result['status'] = 0;
    $result['affect'] = $affect;
    $app->return_json($result);
    exit();
}

// find projects
function find($params, $method, $app) {
    // Check Role
    if (!mod_user\_user_belong(array(ROLE_MANAGER))) {
        exit();
    }
    $start = isset($params['iDisplayStart']) ? intval($params['iDisplayStart']) : 0;
    $size = isset($params['iDisplayLength']) ? intval($params['iDisplayLength']) : 10;
    $result = array();
    $result['sEcho'] = isset($params['sEcho']) ? intval($params['sEcho']) : 1;
        
    # Num Rows
    $total = Project\count();
    if ($total < 1) {
        $result['iTotalRecords'] = 0;
        $result['iTotalDisplayRecords'] = 0;
        $result['aaData'] = array();
        $app->return_json($result);
        exit(); 
    }
    # Find Page
    $projects = Project\find($start, $size);
    # Num Rows
    if(count($projects) < 1) {
        $result['iTotalRecords'] = 0;
        $result['iTotalDisplayRecords'] = 0;
        $result['aaData'] = array();
        $app->return_json($result);
        exit(); 
    }
    
    $result['iTotalRecords'] = $total;
    $result['iTotalDisplayRecords'] = $total;
    $result['aaData'] = array();
    foreach ($projects as $key => $project) {
        $row = array();
        foreach ($project as $key => $value) $row[] = $value;
        $result['aaData'][] = $row;
    }

    $app->return_json($result);
    exit();
}